home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / internet / html-related / hsc / source / hsclib / input.c < prev    next >
C/C++ Source or Header  |  1996-08-03  |  5KB  |  235 lines

  1. /*
  2.  * hsclib/input.c
  3.  *
  4.  * basic functions for parsing input
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 30-Jul-1996
  23.  * created: 29-Jul-1995
  24.  */
  25.  
  26. #include <ctype.h>
  27.  
  28. #include "hsclib/inc_base.h"
  29.  
  30. /*
  31.  *---------------------------
  32.  * overloaded methods for
  33.  * INFILE class
  34.  *---------------------------
  35.  */
  36.  
  37. /*
  38.  * hsc_whtspc
  39.  *
  40.  * decides if an char is a white-space
  41.  *
  42.  * params: ch...char to test
  43.  * result: TRUE, if ch is a 'normal' ch
  44.  *
  45.  * NOTE: this function is used as is_ws-methode
  46.  *       for the infile class
  47.  */
  48. BOOL hsc_whtspc(int ch)
  49. {
  50.     if (strchr(" \t", ch) != NULL)
  51.         return TRUE;
  52.     else
  53.         return FALSE;
  54. }
  55.  
  56. /*
  57.  * hsc_normch
  58.  *
  59.  * decides if an char is an 'normal' char
  60.  *
  61.  * params: ch...char to test
  62.  * result: TRUE, if ch is a 'normal' ch
  63.  *
  64.  * NOTE: this function is used as is_nc-methode
  65.  *       for the infile class
  66.  */
  67. BOOL hsc_normch(int ch)
  68. {
  69. #if 0
  70.     if (isalnum(ch) || (ch == '_') || (ch == '-') || (ch == '.'))
  71.         return TRUE;
  72.     else
  73.         return FALSE;
  74. #else
  75.     if (((ch >= '0') && (ch <= '9'))
  76.         || ((ch >= 'a') && (ch <= 'z'))
  77.         || ((ch >= 'A') && (ch <= 'Z'))
  78.         || (ch == '_') || (ch == '-') || (ch == '.')
  79.         )
  80.         return TRUE;
  81.     else
  82.         return FALSE;
  83. #endif
  84. }
  85.  
  86. /*
  87.  * hsc_normch_tagid
  88.  *
  89.  * decides if an char is an 'normal' char for tagnames
  90.  *
  91.  * params: ch...char to test
  92.  * result: TRUE, if ch is a 'normal' ch
  93.  *
  94.  * NOTE: this function is used as is_nc-methode
  95.  *       for the infile class
  96.  */
  97. BOOL hsc_normch_tagid(int ch)
  98. {
  99.     BOOL found = hsc_normch(ch);
  100.  
  101.     if (!found)
  102.         if (strchr(HSC_TAGID, ch))
  103.             found = TRUE;
  104.  
  105.     return (found);
  106. }
  107.  
  108. /*
  109.  *-------------------------------------
  110.  * read a tag/attribute name from input file
  111.  *-------------------------------------
  112.  */
  113.  
  114. /*
  115.  * infget_tagid
  116.  *
  117.  * read next word from input, but with a
  118.  * different is_nc-methode that also handles
  119.  * the id for hsc-tags (usually "$")
  120.  */
  121. STRPTR infget_tagid(HSCPRC * hp)
  122. {
  123.     INFILE *inpf = hp->inpf;
  124.     STRPTR tagid = NULL;
  125.  
  126.     BOOL(*old_is_nc) (int ch);
  127.  
  128.     old_is_nc = inpf->is_nc;    /* remember old is_nc-methode */
  129.     inpf->is_nc = hsc_normch_tagid;
  130.     tagid = infgetw(inpf);      /* read tagid */
  131.     if (!tagid)
  132.         hsc_msg_eof(hp, "reading tag name");
  133.     inpf->is_nc = old_is_nc;    /* remember old is_nc-methode */
  134.  
  135.     return (tagid);
  136. }
  137.  
  138. /*
  139.  * infget_attrid
  140.  *
  141.  * read next word from input, but with a
  142.  * different is_nc-methode that also handles
  143.  * the id for hsc-attribs (usually "$")
  144.  */
  145. STRPTR infget_attrid(HSCPRC * hp)
  146. {
  147.     INFILE *inpf = hp->inpf;
  148.     STRPTR attrid = NULL;
  149.  
  150.     BOOL(*old_is_nc) (int ch);
  151.  
  152.     old_is_nc = inpf->is_nc;    /* remember old is_nc-methode */
  153.     inpf->is_nc = hsc_normch_tagid;
  154.     attrid = infgetw(inpf);     /* read attrid */
  155.     if (!attrid)
  156.         hsc_msg_eof(hp, "reading attribute name");
  157.     inpf->is_nc = old_is_nc;    /* remember old is_nc-methode */
  158.  
  159.     return (attrid);
  160. }
  161.  
  162. /*
  163.  *-------------------------------------
  164.  * parse simple chars/words
  165.  *-------------------------------------
  166.  */
  167.  
  168. /*
  169.  * parse_wd
  170.  *
  171.  * check if a expected word really occured and
  172.  * display error message if neccessary
  173.  *
  174.  * params: inpf.....input file to read char from
  175.  *         expstr...expected word
  176.  * result: TRUE if successful, FALSE if wrong char found
  177.  */
  178. BOOL parse_wd(HSCPRC * hp, STRPTR expstr)
  179. {
  180.     INFILE *inpf = hp->inpf;
  181.     BOOL value = TRUE;
  182.  
  183.     if (expstr)
  184.     {
  185.  
  186.         STRPTR nw;
  187.  
  188.         /* skip LFs */
  189.         do
  190.             nw = infgetw(inpf);
  191.         while (nw && !strcmp(nw, "\n"));
  192.  
  193.         /* check for expeted word */
  194.         if (!nw || upstrcmp(nw, expstr))
  195.         {
  196.  
  197.             if (!nw)
  198.                 nw = "<EOF>";
  199.  
  200.             hsc_message(hp, MSG_UNEXPT_CH,
  201.                         "expected %q, found %q", expstr, nw);
  202.             value = FALSE;
  203.         }
  204.     }
  205.  
  206.     return (value);
  207. }
  208.  
  209. /*
  210.  * parse_eq
  211.  *
  212.  * check for '='
  213.  *
  214.  * params: inpf...input file to read char from
  215.  * result: -1 if successful, 0 if wrong char found
  216.  */
  217. BOOL parse_eq(HSCPRC * hp)
  218. {
  219.     return (parse_wd(hp, "="));
  220. }
  221.  
  222. /*
  223.  * parse_gt
  224.  *
  225.  * check for '>'
  226.  *
  227.  * params: inpf...input file to read char from
  228.  * result: -1 if successful, 0 if wrong char found
  229.  */
  230. BOOL parse_gt(HSCPRC * hp)
  231. {
  232.     return (parse_wd(hp, ">"));
  233. }
  234.  
  235.